Would like to post a GIF of your agent running in an OpenAI gym environment? It's easy to render a gif through OpenAI gym and embed it in an ipython notebook using the JSAnimation package by Jake Vanderplas. It will render correctly in the Jupyter online viewer and on Github. Neat!

Install like so:

git clone https://github.com/jakevdp/JSAnimation
cd JSAnimation
python setup.py install

Here's a simple demo on an OpenAI gym enviroment:


In [1]:
%matplotlib inline
from JSAnimation.IPython_display import display_animation
from matplotlib import animation
import matplotlib.pyplot as plt
from IPython.display import display

import gym

env = gym.make('CartPole-v0')

# Run a demo of the environment
observation = env.reset()
cum_reward = 0
frames = []
for t in range(1000):
    # Render into buffer. 
    # You will still see the window.
    frames.append(env.render(mode = 'rgb_array'))
    action = env.action_space.sample()
    observation, reward, done, info = env.step(action)
    if done:
        break
env.render(close=True)


[2016-06-21 23:02:41,047] Making new env: CartPole-v0

In [14]:
def display_frames_as_gif(frames):
    """
    Displays a list of frames as a gif, with controls
    """
    plt.figure(figsize=(frames[0].shape[1] / 72.0, frames[0].shape[0] / 72.0), dpi = 72)
    patch = plt.imshow(frames[0])
    plt.axis('off')

    def animate(i):
        patch.set_data(frames[i])

    anim = animation.FuncAnimation(plt.gcf(), animate, frames = len(frames), interval=50)
    display(display_animation(anim, default_mode='loop'))

display_frames_as_gif(frames)




Once Loop Reflect